﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="FuncAuth.aspx.cs" Inherits="WebForm_Auth_FuncAuth" %>

<script runat="server">
    /* list */
    protected void gv_DataBinding(object sender, EventArgs e)
    {
        var sql = new StringBuilder();
        var dbparams = new List<DBParameter>();

        sql.AppendFormat(string.Format(@"SELECT r.*, f.Parent FROM RoleFuncMap r 
                INNER JOIN Func f ON r.FuncID = f.FuncID WHERE RoleID IN ({0}) "
            , MyObj.JoinFormat(",", "'{0}'", RoleManager.Current.GetRolesForUser())));

        if (!string.IsNullOrEmpty(txtRoleID.Text))
        {
            sql.AppendFormat("AND r.RoleID = @RoleID ");
            dbparams.Add(new DBParameter("RoleID", txtRoleID.Text));
        }

        if (listFuncID.Text != string.Empty)
        {
            sql.AppendFormat("AND r.FuncID = @FuncID ");
            dbparams.Add(new DBParameter("FuncID", listFuncID.Text));
        }

        var db = new DB();
        var dt = db.Execute(sql.ToString(), dbparams.ToArray());

        (sender as DataBoundControl).DataSource = dt;

    }

    protected void listRoleID_DataBinding(object sender, EventArgs e)
    {
        (sender as IListControl).DataValueField = "Value";
        (sender as IListControl).DataTextField = "Value";
        (sender as IListControl).DataSource = RoleManager.Current.GetRolesForUser().ToDictionary<string, string>(t => t);
    }

    protected void listFuncID_DataBinding(object sender, EventArgs e)
    {
        (sender as ListControl).DataValueField = "FuncID";
        (sender as ListControl).DataTextField = "FuncName";
        (sender as ListControl).DataSource = (new DB()).Execute(string.Format(@"SELECT f.FuncID, ISNULL(p.{0},'root')+'->'+f.{0} AS FuncName FROM Func f
            LEFT JOIN Func p ON f.Parent = p.FuncID
            WHERE f.FuncID IN ({1})", LocManager.Current.GetTextCol("Func", "FuncName")
                                  , MyObj.JoinFormat(",","{0}", RoleManager.Current.GetFuncsForUser())));
    }
    /* end of list */

    /* form */

    /* end of form */
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:PlaceHolder ID="plQuery" runat="server">
    <table>
        <tr>
            <td class="label">
                <asp:Localize ID="Localize1" runat="server" Text="<%$Resources: Role, RoleID %>"></asp:Localize>
            </td>
            <td class="text">
                <asp:TextBox ID="txtRoleID" runat="server"></asp:TextBox>
                <asp:Panel ID="pnlRoleID" runat="server" SkinID="Popup" Width="300">
                    <asp:UpdatePanel ID="upRoleID" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <My:ListPopup ID="listRoleID" runat="server" OnDataBinding="listRoleID_DataBinding"
                                Clearable="true"></My:ListPopup>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </asp:Panel>
                <ajax:PopupControlExtender ID="peRoleID" runat="server" TargetControlID="txtRoleID"
                    PopupControlID="pnlRoleID" Position="Bottom">
                </ajax:PopupControlExtender>
            </td>
            <td class="label">
                <asp:Localize ID="Localize2" runat="server" Text="<%$Resources: Func, FuncName %>"></asp:Localize>
            </td>
            <td class="text">
                <My:MyDropDownList ID="listFuncID" runat="server" OnDataBinding="listFuncID_DataBinding"
                    EmptyText="<%$Resources: Form, ddl.EmptyText %>">
                </My:MyDropDownList>
            </td>
            <td class="text">
                <asp:Button ID="btnAdd" runat="server" Text="<%$Resources: Form, btnAdd %>" OnClick="btnAdd_Click" />
                <ajax:ConfirmButtonExtender ID="cbAdd" runat="server" TargetControlID="btnAdd" ConfirmText="<%$Resources: Form, cb %>">
                </ajax:ConfirmButtonExtender>
                <asp:Button ID="btnQuery" runat="server" Text="<%$Resources: Form, btnQuery %>" OnClick="btnQuery_Click" />
            </td>
        </tr>
    </table>
</asp:PlaceHolder>
    <My:MyGridView ID="gv" runat="server" OnDataBinding="gv_DataBinding" OnRowCommand="gv_ItemCommand"
        DataKeyNames="FuncID, RoleID">
        <Columns>
            <asp:TemplateField HeaderText="<%$Resources: Role, RoleID   %>">
                <ItemTemplate>
                    <%# Eval("RoleID") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="<%$Resources: Func, FuncName   %>">
                <ItemTemplate>
                    <%# LocManager.Current.GetText("Func", "FuncName", Eval("Parent")) %> ->
                    <%# LocManager.Current.GetText("Func", "FuncName", Eval("FuncID")) %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="btnDelete" runat="server" CommandName="MyDelete" Text="<%$Resources: Form, btnDelete %>"></asp:LinkButton>
                    <ajax:ConfirmButtonExtender ID="cbDelete" runat="server" TargetControlID="btnDelete"
                        ConfirmText="<%$Resources: Form, cbDelete %>">
                    </ajax:ConfirmButtonExtender>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </My:MyGridView>
</asp:Content>
